<?php
/*
	This SQL query will create the table to store your object.

	CREATE TABLE `document` (
	`documentid` int(11) NOT NULL auto_increment,
	`filename` VARCHAR(255) NOT NULL,
	`extension` VARCHAR(255) NOT NULL,
	`type` VARCHAR(255) NOT NULL,
	`title` VARCHAR(255) NOT NULL, PRIMARY KEY  (`documentid`)) ENGINE=MyISAM;
*/

/**
* <b>Document</b> class with integrated CRUD methods.
* @author Php Object Generator
* @version POG 3.0f / PHP5.1 MYSQL
* @see http://www.phpobjectgenerator.com/plog/tutorials/45/pdo-mysql
* @copyright Free for personal & commercial use. (Offered under the BSD license)
* @link http://www.phpobjectgenerator.com/?language=php5.1&wrapper=pdo&pdoDriver=mysql&objectName=Document&attributeList=array+%28%0A++0+%3D%3E+%27filename%27%2C%0A++1+%3D%3E+%27extension%27%2C%0A++2+%3D%3E+%27type%27%2C%0A++3+%3D%3E+%27GotProRole%27%2C%0A++4+%3D%3E+%27GotStudentActivity%27%2C%0A++5+%3D%3E+%27Contact%27%2C%0A++6+%3D%3E+%27SupportCase%27%2C%0A++7+%3D%3E+%27Update%27%2C%0A++8+%3D%3E+%27Activity%27%2C%0A++9+%3D%3E+%27Company%27%2C%0A++10+%3D%3E+%27title%27%2C%0A%29&typeList=array%2B%2528%250A%2B%2B0%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B1%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B2%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2B%2B3%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B4%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B5%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B6%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B7%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B8%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B9%2B%253D%253E%2B%2527JOIN%2527%252C%250A%2B%2B10%2B%253D%253E%2B%2527VARCHAR%2528255%2529%2527%252C%250A%2529
*/
include_once('class.pog_base.php');
include_once('class.documentgotprorolemap.php');
include_once('class.documentgotstudentactivitymap.php');
include_once('class.contactdocumentmap.php');
include_once('class.documentsupportcasemap.php');
include_once('class.documentupdatemap.php');
include_once('class.activitydocumentmap.php');
include_once('class.companydocumentmap.php');
class Document extends POG_Base
{
	public $documentId = '';

	/**
	 * @var VARCHAR(255)
	 */
	public $filename;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $extension;
	
	/**
	 * @var VARCHAR(255)
	 */
	public $type;
	
	/**
	 * @var private array of GotProRole objects
	 */
	private $_gotproroleList = array();
	
	/**
	 * @var private array of GotStudentActivity objects
	 */
	private $_gotstudentactivityList = array();
	
	/**
	 * @var private array of Contact objects
	 */
	private $_contactList = array();
	
	/**
	 * @var private array of SupportCase objects
	 */
	private $_supportcaseList = array();
	
	/**
	 * @var private array of Update objects
	 */
	private $_updateList = array();
	
	/**
	 * @var private array of Activity objects
	 */
	private $_activityList = array();
	
	/**
	 * @var private array of Company objects
	 */
	private $_companyList = array();
	
	/**
	 * @var VARCHAR(255)
	 */
	public $title;
	
	public $pog_attribute_type = array(
		"documentId" => array('db_attributes' => array("NUMERIC", "INT")),
		"filename" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"extension" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"type" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		"GotProRole" => array('db_attributes' => array("OBJECT", "JOIN")),
		"GotStudentActivity" => array('db_attributes' => array("OBJECT", "JOIN")),
		"Contact" => array('db_attributes' => array("OBJECT", "JOIN")),
		"SupportCase" => array('db_attributes' => array("OBJECT", "JOIN")),
		"Update" => array('db_attributes' => array("OBJECT", "JOIN")),
		"Activity" => array('db_attributes' => array("OBJECT", "JOIN")),
		"Company" => array('db_attributes' => array("OBJECT", "JOIN")),
		"title" => array('db_attributes' => array("TEXT", "VARCHAR", "255")),
		);
	public $pog_query;
	
	
	/**
	* Getter for some private attributes
	* @return mixed $attribute
	*/
	public function __get($attribute)
	{
		if (isset($this->{"_".$attribute}))
		{
			return $this->{"_".$attribute};
		}
		else
		{
			return false;
		}
	}
	
	function Document($filename='', $extension='', $type='', $title='')
	{
		$this->filename = $filename;
		$this->extension = $extension;
		$this->type = $type;
		$this->_gotproroleList = array();
		$this->_gotstudentactivityList = array();
		$this->_contactList = array();
		$this->_supportcaseList = array();
		$this->_updateList = array();
		$this->_activityList = array();
		$this->_companyList = array();
		$this->title = $title;
	}
	
	
	/**
	* Gets object from database
	* @param integer $documentId 
	* @return object $Document
	*/
	function Get($documentId)
	{
		$connection = Database::Connect();
		$this->pog_query = "select * from `document` where `documentid`='".intval($documentId)."' LIMIT 1";
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$this->documentId = $row['documentid'];
			$this->filename = $this->Unescape($row['filename']);
			$this->extension = $this->Unescape($row['extension']);
			$this->type = $this->Unescape($row['type']);
			$this->title = $this->Unescape($row['title']);
		}
		return $this;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$connection = Database::Connect();
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$this->pog_query = "select * from `document` ";
		$documentList = Array();
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " where ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($this->pog_attribute_type[$sortBy]['db_attributes']) && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE($sortBy) ";
				}
				else
				{
					$sortBy = "$sortBy ";
				}
			}
			else
			{
				$sortBy = "$sortBy ";
			}
		}
		else
		{
			$sortBy = "documentid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$thisObjectName = get_class($this);
		$cursor = Database::Reader($this->pog_query, $connection);
		while ($row = Database::Read($cursor))
		{
			$document = new $thisObjectName();
			$document->documentId = $row['documentid'];
			$document->filename = $this->Unescape($row['filename']);
			$document->extension = $this->Unescape($row['extension']);
			$document->type = $this->Unescape($row['type']);
			$document->title = $this->Unescape($row['title']);
			$documentList[] = $document;
		}
		return $documentList;
	}
	
	
	/**
	* Saves the object to the database
	* @return integer $documentId
	*/
	function Save($deep = true)
	{
		$connection = Database::Connect();
		$this->pog_query = "select `documentid` from `document` where `documentid`='".$this->documentId."' LIMIT 1";
		$rows = Database::Query($this->pog_query, $connection);
		if ($rows > 0)
		{
			$this->pog_query = "update `document` set 
			`filename`='".$this->Escape($this->filename)."', 
			`extension`='".$this->Escape($this->extension)."', 
			`type`='".$this->Escape($this->type)."', 
			`title`='".$this->Escape($this->title)."' where `documentid`='".$this->documentId."'";
		}
		else
		{
			$this->pog_query = "insert into `document` (`filename`, `extension`, `type`, `title` ) values (
			'".$this->Escape($this->filename)."', 
			'".$this->Escape($this->extension)."', 
			'".$this->Escape($this->type)."', 
			'".$this->Escape($this->title)."' )";
		}
		$insertId = Database::InsertOrUpdate($this->pog_query, $connection);
		if ($this->documentId == "")
		{
			$this->documentId = $insertId;
		}
		if ($deep)
		{
			foreach ($this->_gotproroleList as $gotprorole)
			{
				$gotprorole->Save();
				$map = new DocumentGotProRoleMap();
				$map->AddMapping($this, $gotprorole);
			}
			foreach ($this->_gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->Save();
				$map = new DocumentGotStudentActivityMap();
				$map->AddMapping($this, $gotstudentactivity);
			}
			foreach ($this->_contactList as $contact)
			{
				$contact->Save();
				$map = new ContactDocumentMap();
				$map->AddMapping($this, $contact);
			}
			foreach ($this->_supportcaseList as $supportcase)
			{
				$supportcase->Save();
				$map = new DocumentSupportCaseMap();
				$map->AddMapping($this, $supportcase);
			}
			foreach ($this->_updateList as $update)
			{
				$update->Save();
				$map = new DocumentUpdateMap();
				$map->AddMapping($this, $update);
			}
			foreach ($this->_activityList as $activity)
			{
				$activity->Save();
				$map = new ActivityDocumentMap();
				$map->AddMapping($this, $activity);
			}
			foreach ($this->_companyList as $company)
			{
				$company->Save();
				$map = new CompanyDocumentMap();
				$map->AddMapping($this, $company);
			}
		}
		return $this->documentId;
	}
	
	
	/**
	* Clones the object and saves it to the database
	* @return integer $documentId
	*/
	function SaveNew($deep = false)
	{
		$this->documentId = '';
		return $this->Save($deep);
	}
	
	
	/**
	* Deletes the object from the database
	* @return boolean
	*/
	function Delete($deep = false, $across = false)
	{
		if ($across)
		{
			$gotproroleList = $this->GetGotproroleList();
			$map = new DocumentGotProRoleMap();
			$map->RemoveMapping($this);
			foreach ($gotproroleList as $gotprorole)
			{
				$gotprorole->Delete($deep, $across);
			}
			$gotstudentactivityList = $this->GetGotstudentactivityList();
			$map = new DocumentGotStudentActivityMap();
			$map->RemoveMapping($this);
			foreach ($gotstudentactivityList as $gotstudentactivity)
			{
				$gotstudentactivity->Delete($deep, $across);
			}
			$contactList = $this->GetContactList();
			$map = new ContactDocumentMap();
			$map->RemoveMapping($this);
			foreach ($contactList as $contact)
			{
				$contact->Delete($deep, $across);
			}
			$supportcaseList = $this->GetSupportcaseList();
			$map = new DocumentSupportCaseMap();
			$map->RemoveMapping($this);
			foreach ($supportcaseList as $supportcase)
			{
				$supportcase->Delete($deep, $across);
			}
			$updateList = $this->GetUpdateList();
			$map = new DocumentUpdateMap();
			$map->RemoveMapping($this);
			foreach ($updateList as $update)
			{
				$update->Delete($deep, $across);
			}
			$activityList = $this->GetActivityList();
			$map = new ActivityDocumentMap();
			$map->RemoveMapping($this);
			foreach ($activityList as $activity)
			{
				$activity->Delete($deep, $across);
			}
			$companyList = $this->GetCompanyList();
			$map = new CompanyDocumentMap();
			$map->RemoveMapping($this);
			foreach ($companyList as $company)
			{
				$company->Delete($deep, $across);
			}
		}
		else
		{
			$map = new DocumentGotProRoleMap();
			$map->RemoveMapping($this);
			$map = new DocumentGotStudentActivityMap();
			$map->RemoveMapping($this);
			$map = new ContactDocumentMap();
			$map->RemoveMapping($this);
			$map = new DocumentSupportCaseMap();
			$map->RemoveMapping($this);
			$map = new DocumentUpdateMap();
			$map->RemoveMapping($this);
			$map = new ActivityDocumentMap();
			$map->RemoveMapping($this);
			$map = new CompanyDocumentMap();
			$map->RemoveMapping($this);
		}
		$connection = Database::Connect();
		$this->pog_query = "delete from `document` where `documentid`='".$this->documentId."'";
		return Database::NonQuery($this->pog_query, $connection);
	}
	
	
	/**
	* Deletes a list of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param bool $deep 
	* @return 
	*/
	function DeleteList($fcv_array, $deep = false, $across = false)
	{
		if (sizeof($fcv_array) > 0)
		{
			if ($deep || $across)
			{
				$objectList = $this->GetList($fcv_array);
				foreach ($objectList as $object)
				{
					$object->Delete($deep, $across);
				}
			}
			else
			{
				$connection = Database::Connect();
				$pog_query = "delete from `document` where ";
				for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
				{
					if (sizeof($fcv_array[$i]) == 1)
					{
						$pog_query .= " ".$fcv_array[$i][0]." ";
						continue;
					}
					else
					{
						if ($i > 0 && sizeof($fcv_array[$i-1]) !== 1)
						{
							$pog_query .= " AND ";
						}
						if (isset($this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $this->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$this->Escape($fcv_array[$i][2])."'";
						}
						else
						{
							$pog_query .= "`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." '".$fcv_array[$i][2]."'";
						}
					}
				}
				return Database::NonQuery($pog_query, $connection);
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the GotProRole List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetGotproroleList(&$gotproroleList)
	{
		$map = new DocumentGotProRoleMap();
		$map->RemoveMapping($this);
		$this->_gotproroleList = $gotproroleList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetGotproroleList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$gotprorole = new GotProRole();
		$gotproroleList = Array();
		$this->pog_query = "select distinct * from `gotprorole` a INNER JOIN `documentgotprorolemap` m ON m.gotproroleid = a.gotproroleid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($gotprorole->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $gotprorole->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $gotprorole->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($gotprorole->pog_attribute_type[$sortBy]['db_attributes']) && $gotprorole->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $gotprorole->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.gotproroleid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$gotprorole = new GotProRole();
			foreach ($gotprorole->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$gotprorole->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$gotprorole->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$gotproroleList[] = $gotprorole;
		}
		return $gotproroleList;
	}
	
	
	/**
	* Associates the GotProRole object to this one
	* @return 
	*/
	function AddGotprorole(&$gotprorole)
	{
		if ($gotprorole instanceof GotProRole)
		{
			if (in_array($this, $gotprorole->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_gotproroleList as $gotprorole2)
				{
					if ($gotprorole->gotproroleId > 0 && $gotprorole->gotproroleId == $gotprorole2->gotproroleId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_gotproroleList[] = $gotprorole;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the GotStudentActivity List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetGotstudentactivityList(&$gotstudentactivityList)
	{
		$map = new DocumentGotStudentActivityMap();
		$map->RemoveMapping($this);
		$this->_gotstudentactivityList = $gotstudentactivityList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetGotstudentactivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$gotstudentactivity = new GotStudentActivity();
		$gotstudentactivityList = Array();
		$this->pog_query = "select distinct * from `gotstudentactivity` a INNER JOIN `documentgotstudentactivitymap` m ON m.gotstudentactivityid = a.gotstudentactivityid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($gotstudentactivity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $gotstudentactivity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $gotstudentactivity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($gotstudentactivity->pog_attribute_type[$sortBy]['db_attributes']) && $gotstudentactivity->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $gotstudentactivity->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.gotstudentactivityid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$gotstudentactivity = new GotStudentActivity();
			foreach ($gotstudentactivity->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$gotstudentactivity->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$gotstudentactivity->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$gotstudentactivityList[] = $gotstudentactivity;
		}
		return $gotstudentactivityList;
	}
	
	
	/**
	* Associates the GotStudentActivity object to this one
	* @return 
	*/
	function AddGotstudentactivity(&$gotstudentactivity)
	{
		if ($gotstudentactivity instanceof GotStudentActivity)
		{
			if (in_array($this, $gotstudentactivity->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_gotstudentactivityList as $gotstudentactivity2)
				{
					if ($gotstudentactivity->gotstudentactivityId > 0 && $gotstudentactivity->gotstudentactivityId == $gotstudentactivity2->gotstudentactivityId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_gotstudentactivityList[] = $gotstudentactivity;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Contact List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetContactList(&$contactList)
	{
		$map = new ContactDocumentMap();
		$map->RemoveMapping($this);
		$this->_contactList = $contactList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetContactList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$contact = new Contact();
		$contactList = Array();
		$this->pog_query = "select distinct * from `contact` a INNER JOIN `contactdocumentmap` m ON m.contactid = a.contactid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($contact->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $contact->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $contact->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($contact->pog_attribute_type[$sortBy]['db_attributes']) && $contact->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $contact->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.contactid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$contact = new Contact();
			foreach ($contact->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$contact->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$contact->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$contactList[] = $contact;
		}
		return $contactList;
	}
	
	
	/**
	* Associates the Contact object to this one
	* @return 
	*/
	function AddContact(&$contact)
	{
		if ($contact instanceof Contact)
		{
			if (in_array($this, $contact->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_contactList as $contact2)
				{
					if ($contact->contactId > 0 && $contact->contactId == $contact2->contactId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_contactList[] = $contact;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the SupportCase List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetSupportcaseList(&$supportcaseList)
	{
		$map = new DocumentSupportCaseMap();
		$map->RemoveMapping($this);
		$this->_supportcaseList = $supportcaseList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetSupportcaseList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$supportcase = new SupportCase();
		$supportcaseList = Array();
		$this->pog_query = "select distinct * from `supportcase` a INNER JOIN `documentsupportcasemap` m ON m.supportcaseid = a.supportcaseid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($supportcase->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $supportcase->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $supportcase->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($supportcase->pog_attribute_type[$sortBy]['db_attributes']) && $supportcase->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $supportcase->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.supportcaseid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$supportcase = new SupportCase();
			foreach ($supportcase->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$supportcase->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$supportcase->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$supportcaseList[] = $supportcase;
		}
		return $supportcaseList;
	}
	
	
	/**
	* Associates the SupportCase object to this one
	* @return 
	*/
	function AddSupportcase(&$supportcase)
	{
		if ($supportcase instanceof SupportCase)
		{
			if (in_array($this, $supportcase->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_supportcaseList as $supportcase2)
				{
					if ($supportcase->supportcaseId > 0 && $supportcase->supportcaseId == $supportcase2->supportcaseId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_supportcaseList[] = $supportcase;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Update List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetUpdateList(&$updateList)
	{
		$map = new DocumentUpdateMap();
		$map->RemoveMapping($this);
		$this->_updateList = $updateList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetUpdateList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$update = new Update();
		$updateList = Array();
		$this->pog_query = "select distinct * from `update` a INNER JOIN `documentupdatemap` m ON m.updateid = a.updateid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($update->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $update->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $update->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($update->pog_attribute_type[$sortBy]['db_attributes']) && $update->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $update->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.updateid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$update = new Update();
			foreach ($update->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$update->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$update->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$updateList[] = $update;
		}
		return $updateList;
	}
	
	
	/**
	* Associates the Update object to this one
	* @return 
	*/
	function AddUpdate(&$update)
	{
		if ($update instanceof Update)
		{
			if (in_array($this, $update->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_updateList as $update2)
				{
					if ($update->updateId > 0 && $update->updateId == $update2->updateId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_updateList[] = $update;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Activity List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetActivityList(&$activityList)
	{
		$map = new ActivityDocumentMap();
		$map->RemoveMapping($this);
		$this->_activityList = $activityList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetActivityList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$activity = new Activity();
		$activityList = Array();
		$this->pog_query = "select distinct * from `activity` a INNER JOIN `activitydocumentmap` m ON m.activityid = a.activityid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($activity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $activity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $activity->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($activity->pog_attribute_type[$sortBy]['db_attributes']) && $activity->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $activity->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.activityid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$activity = new Activity();
			foreach ($activity->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$activity->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$activity->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$activityList[] = $activity;
		}
		return $activityList;
	}
	
	
	/**
	* Associates the Activity object to this one
	* @return 
	*/
	function AddActivity(&$activity)
	{
		if ($activity instanceof Activity)
		{
			if (in_array($this, $activity->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_activityList as $activity2)
				{
					if ($activity->activityId > 0 && $activity->activityId == $activity2->activityId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_activityList[] = $activity;
				}
			}
		}
	}
	
	
	/**
	* Creates mappings between this and all objects in the Company List array. Any existing mapping will become orphan(s)
	* @return null
	*/
	function SetCompanyList(&$companyList)
	{
		$map = new CompanyDocumentMap();
		$map->RemoveMapping($this);
		$this->_companyList = $companyList;
	}
	
	
	/**
	* Returns a sorted array of objects that match given conditions
	* @param multidimensional array {("field", "comparator", "value"), ("field", "comparator", "value"), ...} 
	* @param string $sortBy 
	* @param boolean $ascending 
	* @param int limit 
	* @return array $documentList
	*/
	function GetCompanyList($fcv_array = array(), $sortBy='', $ascending=true, $limit='')
	{
		$sqlLimit = ($limit != '' ? "LIMIT $limit" : '');
		$connection = Database::Connect();
		$company = new Company();
		$companyList = Array();
		$this->pog_query = "select distinct * from `company` a INNER JOIN `companydocumentmap` m ON m.companyid = a.companyid where m.documentid = '$this->documentId' ";
		if (sizeof($fcv_array) > 0)
		{
			$this->pog_query .= " AND ";
			for ($i=0, $c=sizeof($fcv_array); $i<$c; $i++)
			{
				if (sizeof($fcv_array[$i]) == 1)
				{
					$this->pog_query .= " ".$fcv_array[$i][0]." ";
					continue;
				}
				else
				{
					if ($i > 0 && sizeof($fcv_array[$i-1]) != 1)
					{
						$this->pog_query .= " AND ";
					}
					if (isset($company->pog_attribute_type[$fcv_array[$i][0]]['db_attributes']) && $company->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'NUMERIC' && $company->pog_attribute_type[$fcv_array[$i][0]]['db_attributes'][0] != 'SET')
					{
						if ($GLOBALS['configuration']['db_encoding'] == 1)
						{
							$value = POG_Base::IsColumn($fcv_array[$i][2]) ? "BASE64_DECODE(".$fcv_array[$i][2].")" : "'".$fcv_array[$i][2]."'";
							$this->pog_query .= "BASE64_DECODE(`".$fcv_array[$i][0]."`) ".$fcv_array[$i][1]." ".$value;
						}
						else
						{
							$value =  POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$this->Escape($fcv_array[$i][2])."'";
							$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
						}
					}
					else
					{
						$value = POG_Base::IsColumn($fcv_array[$i][2]) ? $fcv_array[$i][2] : "'".$fcv_array[$i][2]."'";
						$this->pog_query .= "a.`".$fcv_array[$i][0]."` ".$fcv_array[$i][1]." ".$value;
					}
				}
			}
		}
		if ($sortBy != '')
		{
			if (isset($company->pog_attribute_type[$sortBy]['db_attributes']) && $company->pog_attribute_type[$sortBy]['db_attributes'][0] != 'NUMERIC' && $company->pog_attribute_type[$sortBy]['db_attributes'][0] != 'SET')
			{
				if ($GLOBALS['configuration']['db_encoding'] == 1)
				{
					$sortBy = "BASE64_DECODE(a.$sortBy) ";
				}
				else
				{
					$sortBy = "a.$sortBy ";
				}
			}
			else
			{
				$sortBy = "a.$sortBy ";
			}
		}
		else
		{
			$sortBy = "a.companyid";
		}
		$this->pog_query .= " order by ".$sortBy." ".($ascending ? "asc" : "desc")." $sqlLimit";
		$cursor = Database::Reader($this->pog_query, $connection);
		while($rows = Database::Read($cursor))
		{
			$company = new Company();
			foreach ($company->pog_attribute_type as $attribute_name => $attrubute_type)
			{
				if ($attrubute_type['db_attributes'][1] != "HASMANY" && $attrubute_type['db_attributes'][1] != "JOIN")
				{
					if ($attrubute_type['db_attributes'][1] == "BELONGSTO")
					{
						$company->{strtolower($attribute_name).'Id'} = $rows[strtolower($attribute_name).'id'];
						continue;
					}
					$company->{$attribute_name} = $this->Unescape($rows[strtolower($attribute_name)]);
				}
			}
			$companyList[] = $company;
		}
		return $companyList;
	}
	
	
	/**
	* Associates the Company object to this one
	* @return 
	*/
	function AddCompany(&$company)
	{
		if ($company instanceof Company)
		{
			if (in_array($this, $company->documentList, true))
			{
				return false;
			}
			else
			{
				$found = false;
				foreach ($this->_companyList as $company2)
				{
					if ($company->companyId > 0 && $company->companyId == $company2->companyId)
					{
						$found = true;
						break;
					}
				}
				if (!$found)
				{
					$this->_companyList[] = $company;
				}
			}
		}
	}
}
?>